<template>
  <div class="todo-footer" v-show="isAll">
        <label>
          <input type="checkbox" :checked="checkAll" @change="clickCheckAll"/>
        </label>
        <span>
          <span>已完成{{doneAmount}}</span> / 全部{{isAll}}
        </span>
        <button class="btn btn-danger" @click="deletDone">清除已完成任务</button>
  </div>
</template>

<script>
export default {
  name:'MyFooter',
  props:['todoList','clickCheckTodo','dedletDoneTodo'],
  computed:{
    // 计算底部已经完成的数量
    doneAmount(){
      // reduce两个参数。第一个是初始值，第二个是遍历的对象
      return this.todoList.reduce((pre,current)=>{
        return pre+(current.done?1:0)
      },0)
    },
    // 全部
    isAll(){
      return this.todoList.length
    },
    // 是否全选
    checkAll(){
      return this.doneAmount===this.isAll && this.isAll>0
    }
  },
  methods:{
    // 点击全选每一项
    clickCheckAll(e){
      // 从App获取数据
      this.clickCheckTodo(e.target.checked)
    },
    // 清除已经完成的
    deletDone(){
      this.dedletDoneTodo()
    }
  }
}
</script>

<style scoped>
/*footer*/
.todo-footer {
  height: 40px;
  line-height: 40px;
  padding-left: 6px;
  margin-top: 5px;
}

.todo-footer label {
  display: inline-block;
  margin-right: 20px;
  cursor: pointer;
}

.todo-footer label input {
  position: relative;
  top: -1px;
  vertical-align: middle;
  margin-right: 5px;
}

.todo-footer button {
  float: right;
  margin-top: 5px;
}
</style>